using System;
using System.IO;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.NotificationServices
{
	internal class InputBuffer
	{
		public const string END_OF_STREAM_MESSAGE = "End of data encountered.";

		private Stream inputStream;

		protected internal virtual string NextString
		{
			get
			{
				StringBuilder stringBuilder = new StringBuilder();
				string result = null;
				while (true)
				{
					int num = inputStream.ReadByte();
					if (num < 0)
					{
						throw new IOException("End of data encountered.");
					}
					if (num == 13)
					{
						break;
					}
					stringBuilder.Append((char)num);
				}
				inputStream.ReadByte();
				if (stringBuilder.Length > 0)
				{
					result = stringBuilder.ToString();
				}
				return result;
			}
		}

		protected internal InputBuffer(Stream inputStream)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.InputBuffer, OracleTraceFuncName.ctor);
			}
			this.inputStream = inputStream;
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.InputBuffer, OracleTraceFuncName.ctor);
			}
		}

		protected internal virtual int getBytes(sbyte[] buf, int len)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.InputBuffer, OracleTraceFuncName.getBytes);
			}
			int num = 0;
			try
			{
				for (int i = 0; i < len; i++)
				{
					int num2 = inputStream.ReadByte();
					if (num2 < 0)
					{
						return -1;
					}
					buf[i] = (sbyte)num2;
					num++;
				}
				return num;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.InputBuffer, OracleTraceFuncName.getBytes, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.InputBuffer, OracleTraceFuncName.getBytes);
				}
			}
		}

		protected internal virtual int skipBytes(int len)
		{
			int num = 0;
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.InputBuffer, OracleTraceFuncName.skipBytes);
			}
			try
			{
				for (int i = 0; i < len; i++)
				{
					if (inputStream.ReadByte() < 0)
					{
						throw new IOException("End of data encountered.");
					}
					num++;
				}
				return num;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.InputBuffer, OracleTraceFuncName.skipBytes, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.InputBuffer, OracleTraceFuncName.skipBytes);
				}
			}
		}
	}
}
